%Program to read in RTS file, calibrate and write as ascii fil
%latest date 19.12.05

function[] = mmts();
myBlock = [256,512,1024,2048,4096];
%[fid,er,file] = getbfile('*.rts');
[fid,er,file,path1]=GetBfile('*.rts');
	if fid == -1 | er < 1,
      return;
	end;
	disp('Rts-in -> reading time series');
	[TS,fid] = rts_in(fid); % reading time series file (rts format - metronix)
   fclose(fid);
   ProcDef = SetDefault(TS.head.band);
   ProcDef.file1=file;
 %  ProcDef.block = 1024; %!!!!!! REMOVE THIS LINE 24.7.05 TOULOUSE
   ProcDef.len = floor(length(TS.matrix)/ProcDef.block);
   ProcDef.SelStacks = ones([1 ProcDef.len]);
   flag = 1;
   blk =0;
   
   ans_aa = ['Y','N'];
   aa = 'Q';
   while ismember(upper(aa),ans_aa) ~= 1,
   aa = input('Require trend and bias elemination ? (Y/N) \n','s');
    end;
   
   if upper(aa) == 'Y',
       if TS.head.band > 2,
            fprintf('Enter the block size for trend/bias elimination\n');
            fprintf('1 for 256\n 2 for 512\n 3 for 1024\n 4 for 2048\n 5 for 4096\n');
             while ismember(blk,[1,2,3,4,5]) ~= 1,
                blk = input('Your choise   ');
            end;
      ProcDef.block = myBlock(blk);
        else,
            fprintf('Enter the block size for trend/bias elimination\n');
            fprintf('1 for 256\n 2 for 512\n 3 for 1024\n');
             while ismember(blk,[1,2,3]) ~= 1,
                blk = input('Your choise   ');
            end;
             ProcDef.block = myBlock(blk);

        end;
    end;
    
ProcDef.len = floor(length(TS.matrix)/ProcDef.block);

nchannel =5;
plchan=5;
block = ProcDef.block;

for i = 1:TS.head.nchannel,
factor(i) = NrFct(TS.head.band,ProcDef.sensor(i),TS.probe.dist(i),TS.probe.gain(i));
end;


factor(1:2) = factor(1:2)*0.040; %these factors were found necessary while 
factor(3:5) = factor(3:5)*5; % comparing with ProcMT 22.5.3

for i = 1:nchannel,
TS.matrix(:,i) = TS.matrix(:,i).*factor(i);
end;

if upper(aa) == 'Y',
    
    len = ProcDef.len;
    start = 0;
    st = start*block + 1;
    en = (start+1)*block;
    nchannel = 5;

    for i = (start+1):len,
        for u = 1:5,
	        TS.matrix(st:en,u) = CorTre(TS.matrix(st:en,u),0);
        end;
    st = en + 1;
    en = (i+1)*block;
    end;
end;

fid = fopen([path1 file(1:end-3) 'asc'],'wt');

for i = 1:length(TS.matrix),
    fprintf(fid,'%e %e %e %e %e\n',TS.matrix(i,1),TS.matrix(i,2),TS.matrix(i,3),TS.matrix(i,4),TS.matrix(i,5));
end;

fprintf('Successfully written ascii file %s .\n', [path1 file(1:end-3) 'asc']);
fprintf('Ex and Ey are in mV/km and Hx, Hy and Hz are in nT\n');
if upper(aa) == 'Y',
    fprintf('Note ! The time series should be plotted as blocks of %d \n', ProcDef.block);
end;
if  TS.head.band > 2
    fprintf('If you want to plot the TS continuously (applicable to Band 3 and above only),  use the option N in the trend and bias elimination query\n');
end;

fclose(fid);



